import { colors, getCssVar,date } from 'quasar'
import { computed, ref } from "vue"

//根据时间戳格式化时间
export const formatTimeDetail = (timestamp) => {
  const time = new Date(timestamp.toString().length === 10 ? timestamp * 1000 : timestamp)
  const now = new Date()
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  const messageDate = new Date(time.getFullYear(), time.getMonth(), time.getDate())

  const diffDays = Math.floor((today - messageDate) / (1000 * 60 * 60 * 24))

  if (diffDays === 0) {
    // 今天，显示时间
    return time.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit',
    })
  } else if (diffDays === 1) {
    // 昨天
    return (
      '昨天 ' +
      time.toLocaleTimeString('zh-CN', {
        hour: '2-digit',
        minute: '2-digit',
      })
    )
  } else if (diffDays < 7) {
    // 一周内
    const weekdays = ['日', '一', '二', '三', '四', '五', '六']
    return (
      '周' +
      weekdays[time.getDay()] +
      ' ' +
      time.toLocaleTimeString('zh-CN', {
        hour: '2-digit',
        minute: '2-digit',
      })
    )
  } else {
    // 超过一周
    return (
      time.toLocaleDateString('zh-CN') +
      ' ' +
      time.toLocaleTimeString('zh-CN', {
        hour: '2-digit',
        minute: '2-digit',
      })
    )
  }
}

export const formatTime = (timestamp) => {
  const now = new Date()
  // 处理时间戳格式（支持秒级和毫秒级时间戳）
  const time = new Date(timestamp.toString().length === 10 ? timestamp * 1000 : timestamp)
  const diffMs = now - time
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60))
  const diffDays = Math.floor(diffHours / 24)

  if (diffHours < 1) {
    const diffMinutes = Math.floor(diffMs / (1000 * 60))
    return diffMinutes <= 0 ? '刚刚' : `${diffMinutes}分钟前`
  } else if (diffHours < 24) {
    return `${diffHours}小时前`
  } else if (diffDays === 1) {
    return '昨天'
  } else if (diffDays < 7) {
    return `${diffDays}天前`
  } else if (diffDays >= 365) {
    return date.formatDate(time, 'YYYY/MM/DD')
  } else if (diffDays >= 30) {
    const diffMonths = Math.floor(diffDays / 30)
    return `${diffMonths}月前`
  } else {
    return date.formatDate(time, 'MM/DD')
  }
}

const { lighten } = colors
export const unreadColor = ref(lighten(getCssVar('primary'), 95)) //浅色
export const unreadColorDark = ref(lighten(getCssVar('primary'), -75)) //深色


//设置毛玻璃效果
export const setTransColor = ()=>{
  const hex = getCssVar('primary')+'ad'
  return {
    backgroundColor: `${hex}`,
    backdropFilter:`blur(20px)`,
  }
}

//判断安卓环境15
export const isAndroid15Plus = computed(()=>{
  if (Capacitor.getPlatform() === 'android') {
    const userAgent = navigator.userAgent;
    
    // 解析User Agent中的Android版本
    // 格式通常为: "Android 15" 或 "Android 15.0"
    const androidMatch = userAgent.match(/Android\s+(\d+)/);
    if (androidMatch) {
      const androidVersion = parseInt(androidMatch[1]);
      return androidVersion >= 15;
    }
  }
  return false;
})